package Screens;

import java.util.ArrayList;

import Simulation.Gem;
import Simulation.Player;
import Simulation.Simulation;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData;
import com.badlogic.gdx.graphics.g2d.stbtt.TrueTypeFontFactory;
import com.badlogic.gdx.math.Vector3;
import com.me.mynanogame.Main;
import com.me.mynanogame.Renderer;
import com.me.mynanogame.StaticSprites;


public class Negotiation extends Screen{
	private Main main;
	public Vector3 translationVector;
	public Renderer render;
	public float xPos, yPos, orgXPos, orgYPos;
	public boolean startedGame, resetActiveGem, resetActiveGem2, allTaken, touchDown;
	public float negTimer;
	public float opaqueTimer;
	public ArrayList<Player> players = new ArrayList<Player>();;
	public Simulation sim;
	public Sprite activeGem;
	public Gem activeG;
	public BitmapFont font;
	public BitmapFont font22;
	public int amountOfGems;

	public Negotiation(Main m, Renderer r, Simulation s){
		main = m;
		render = r;
		translationVector = new Vector3();
		xPos = 0;
		yPos = 0;
		orgXPos = 0;
		orgYPos = 0;
		startedGame = true;
		negTimer = 60f;
		opaqueTimer = 1f;
		initiatePlayers();
		sim = s;
		amountOfGems = 8;
		initSim(amountOfGems);
		activeGem = null;
		activeG = null;
		resetActiveGem = false;
		allTaken = false;
		touchDown = false;
		players.get(2).changeNeg(false);
		StaticSprites.dragon2.setSize(StaticSprites.dragon2.getWidth()/1.75f, StaticSprites.dragon2.getHeight()/1.75f);
		font = new BitmapFont(Gdx.files.internal("data/font.fnt"),
				Gdx.files.internal("data/font.png"), false);
		font22 = new BitmapFont(Gdx.files.internal("data/font22.fnt"),
				Gdx.files.internal("data/font22.png"), false);
	}

	public void initSim(int amountGems){
		sim.simulate(players, amountGems, 0);
		for(int i = 0; i < amountGems; i++){
			sim.activeGems.get(i).gemSprite = sim.activeGems.get(i).getGem(i);
		}
	}


	public void initiatePlayers(){
		Player p1 = new Player(0);
		Player p2 = new Player(1);
		Player p3 = new Player(2);
		Player p4 = new Player(3);
		Player p5 = new Player(4);
		Player p6 = new Player(5);
		players.add(p1);
		players.add(p2);
		players.add(p3);
		players.add(p4);
		players.add(p5);
		players.add(p6);
	}

	@Override
	public boolean keyDown(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		translationVector.set(x,y,0);
		render.cam.unproject(translationVector);
		xPos = translationVector.x*0.72f;
		yPos = translationVector.y*0.72f;
		return false;
	}

	public void updateActiveGem(){
		for(int i = 0; i < sim.activeGems.size(); i++){
			if(sim.activeGems.get(i).gemSprite.getBoundingRectangle().contains(xPos, yPos)){
				activeGem = sim.activeGems.get(i).gemSprite;
				activeG = sim.activeGems.get(i);
			}
		}
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		translationVector.set(x,y,0);
		render.cam.unproject(translationVector);
		xPos = translationVector.x*0.72f;
		yPos = translationVector.y*0.72f;
		for(int i = 0; i < players.size(); i++){
			if(players.get(i).getVisibleStash().getBoundingRectangle().contains(xPos, yPos) && players.get(i).getNeg()
					&& !players.get(i).playerGems.contains(activeG)){
				//System.out.println("Player: " + i);
				for(int j = 0; j < sim.activeGems.size(); j++){
					if(activeG == sim.activeGems.get(j)){
						sim.activeGems.get(j).gemSprite.setPosition(xPos-15, yPos-15);
						sim.activeGems.get(j).setTaken(true);
						//availibleGems[j].setPosition(xPos, yPos);
						//System.out.println(sim.activeGems.get(j).taken);
					}
				}				
				resetActiveGem = true;
				return false;
			}
			else{
				for(int j = 0; j < sim.activeGems.size(); j++){
					if(activeG == sim.activeGems.get(j)){
						sim.activeGems.get(j).setTaken(false);
						players.get(i).playerGems.remove(activeG);
						updateGemP(i);
						//System.out.println(sim.activeGems.get(j).taken);
						//sim.activeGems.get(j).gemSprite.setPosition(xPos, yPos);
						//availibleGems[j].setPosition(xPos, yPos);
						//System.out.println(availibleGems[j].getX());
					}
				}

			}
		}
		resetActiveGem = true;
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		translationVector.set(x,y,0);
		render.cam.unproject(translationVector);
		xPos = translationVector.x*0.72f;
		yPos = translationVector.y*0.72f;
		return false;
	}

	@Override
	public boolean touchMoved(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}

	public void updateGemP(int player){
		int i = 1;
		if(player == 0){
			for(Gem ge : players.get(player).playerGems){
				ge.gemSprite.setPosition(ge.gemSprite.getWidth()*(i), 155);
				i++;
			}
		}
		else{
			for(Gem g : players.get(player).playerGems){
				g.gemSprite.setPosition(players.get(0).getVisibleStash().getWidth()
						+ (5 + StaticSprites.visibleStack[0].getWidth())*(player-1)
						+ g.gemSprite.getWidth()*(i), 100);
				i++;
			}
		}
	}



	public void setGemPositions(){
		int i = 0;
		for(Gem g : sim.activeGems){
			if(g.taken){
				for(int j = 0; j < players.size(); j++){
					if(players.get(j).getVisibleStash().getBoundingRectangle().contains(g.gemSprite.getX(), g.gemSprite.getY()) && players.get(j).getNeg()){
						if(j == 0){
							int a = 1;
							for(Gem ge : players.get(j).playerGems){
								ge.gemSprite.setPosition(ge.gemSprite.getWidth()*(a), 155);
								a++;
							}
						}
						else{
							int b = 1;
							for(Gem ge : players.get(j).playerGems){
								ge.gemSprite.setPosition(players.get(0).getVisibleStash().getWidth()
										+ (5 + players.get(j).getVisibleStash().getWidth())*(j-1)+
										ge.gemSprite.getWidth()*(b), 100);
								b++;
							}

						}
					}
				}
			}
			else{
				g.gemSprite.setPosition(Gdx.graphics.getWidth()/2f+30*(i)-15*sim.activeGems.size(), 175);
			}
			i++;
		}
	}

	public void checkAllTaken(){
		int amount = 0;
		for(int i = 0; i < sim.activeGems.size(); i++){
			if(sim.activeGems.get(i).taken){
				amount++;
			}
			if(amount == sim.activeGems.size()){
				allTaken = true;
			}
			else{
				allTaken = false;
			}
		}
	}

	@Override
	public void update() {
		if(startedGame){
			negTimer -= Gdx.graphics.getDeltaTime();
			opaqueTimer = (negTimer/100)/0.6f;
			if(negTimer <= 0){
				opaqueTimer = 0;
				negTimer = 60;
				startedGame = false;
			}
		}
		try{
			//System.out.println(waitUntilRelease);
			updateActiveGem();
			setGemPositions();
			checkAllTaken();
		}
		catch(NullPointerException e){
			e.getStackTrace();
		}
		if(resetActiveGem){
			activeG = null;
			activeGem = null;
			resetActiveGem = false;
		}

	}

	@Override
	public void render() {
		Renderer.getInstance().renderGame(this);

	}

	public void renderGame(SpriteBatch sb){
		StaticSprites.wallPaper.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		StaticSprites.wallPaper.draw(sb, 1f);
		StaticSprites.deadDragon.setSize(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2.5f);
		StaticSprites.deadDragon.setPosition(Gdx.graphics.getWidth()/2f+StaticSprites.deadDragon.getWidth()/10f, Gdx.graphics.getHeight()/8f);
		StaticSprites.deadDragon.draw(sb, opaqueTimer);
		StaticSprites.dragonBG.setPosition(35, 400);
		StaticSprites.dragonBG.draw(sb, 1f);
		StaticSprites.dragon2.setPosition(15, 430);
		StaticSprites.dragon2.draw(sb, 1f);
		StaticSprites.protossPS.setPosition(80, 320);
		StaticSprites.protossPS.draw(sb, 0.8f);

		
		players.get(0).getVisibleStash().setPosition(10, 10);
		players.get(0).getVisibleStash().draw(sb, 0.8f);
		for(int i = 1; i < players.size(); i++){
			if(players.get(i).getNeg()){
				players.get(i).getVisibleStash().setPosition(StaticSprites.player1Stack.getWidth()/2+ players.get(i).getVisibleStash().getWidth()*(i)+5*i, 10);
				players.get(i).getVisibleStash().draw(sb, 0.8f);
			}
			else{
				players.get(i).getVisibleStash().setPosition(StaticSprites.player1Stack.getWidth()/2+ players.get(i).getVisibleStash().getWidth()*(i)+5*i, 10);
				players.get(i).getVisibleStash().draw(sb, 0.2f);
			}

		}
		for(int i = 0; i < players.size(); i++){
			players.get(i).getPortrait().setPosition((Gdx.graphics.getWidth()-players.get(i).getPortrait().getWidth()*(players.size()))/2, Gdx.graphics.getHeight()-players.get(i).getPortrait().getHeight()*1.1f*(i+1));
			players.get(i).getPortrait().draw(sb, 0.8f);
		}
		if(sim.simulated){
			for(int i = 0; i < sim.activeGems.size(); i++){
				float opac = 1.0f;
				if(sim.activeGems.get(i) == activeG){
					opac = 0.5f;
				}
				try{
					sim.activeGems.get(i).gemSprite.draw(sb, opac);
				}
				catch(NullPointerException e){
					e.getStackTrace();
				}
			}

		}
		if(activeGem != null){
			activeGem.setPosition(xPos-15,yPos-15);
			activeGem.draw(sb);
		}
		
		//silver gold red GUI gems
		StaticSprites.silver[19].setPosition(30, 85);
		StaticSprites.silver[19].draw(sb, 1f);
		StaticSprites.gold[19].setPosition(80, 85);
		StaticSprites.gold[19].draw(sb, 1f);
		StaticSprites.red[19].setPosition(130, 85);
		StaticSprites.red[19].draw(sb, 1f);
		font22.draw(sb, "0", 60, 105);
		font22.draw(sb, "0", 110, 105);
		font22.draw(sb, "0", 160, 105);
		//black green blue GUI gems
		StaticSprites.blackGUI.setPosition(30, 55);
		StaticSprites.blackGUI.draw(sb, 1f);
		StaticSprites.green[19].setPosition(80, 55);
		StaticSprites.green[19].draw(sb, 1f);
		StaticSprites.blue[19].setPosition(130, 55);
		StaticSprites.blue[19].draw(sb, 1f);
		font22.draw(sb, "0", 60, 75);
		font22.draw(sb, "0", 110, 75);
		font22.draw(sb, "0", 160, 75);
		//purple yellow white GUI gems
		StaticSprites.purple[19].setPosition(30, 25);
		StaticSprites.purple[19].draw(sb, 1f);
		StaticSprites.yellow[19].setPosition(80, 25);
		StaticSprites.yellow[19].draw(sb, 1f);
		StaticSprites.white[19].setPosition(130, 25);
		StaticSprites.white[19].draw(sb, 1f);			
		font22.draw(sb, "0", 60, 45);
		font22.draw(sb, "0", 110, 45);
		font22.draw(sb, "0", 160, 45);
		
		//drawing fonts
		font.draw(sb, "SLAIN", 75, 430);
		font.draw(sb, "0", 105, 475);
		font.draw(sb, "Andreas L", 65, 140);
		font22.draw(sb, "Li", 290, 85);
		font22.setColor(1.0f, 1.0f, 1.0f, 0.2f);
		font22.draw(sb, "Hanna", 405, 85);
		font22.setColor(1.0f, 1.0f, 1.0f, 1.0f);
		font22.draw(sb, "Steven", 535, 85);
		font22.draw(sb, "Andreas K", 650, 85);
		font22.draw(sb, "Jorgen", 800, 85);
		//font.draw(sb, "Andreas L", StaticSprites.player1Stack.getWidth()/2-font.getBounds("Andreas L").width/2, StaticSprites.player1Stack.getHeight());
	}

	@Override
	public void enter() {
		Gdx.input.setCatchBackKey(true);
		Gdx.input.setInputProcessor(this);

	}

}
